﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;

namespace JingQiao.Ads.Infrastructure.Data.EF.Repository
{
    using Domain.Model;
    using Domain.Model.DomainObject;
    using Domain.Model.Repository;
 
    public class DestPositionAdRepository : BaseRepository<IDestPositionAd>, IDestPositionAdRepository
    {
        public DestPositionAdRepository(IContext context)
            : base(context)
        {
        }

        public IQueryable<IDestPositionAd> FindByAdvertisementId(int advertisementId)
        {
            var destPositionAds = this._context.DestPositionAd.Include("Destination").Where(d => d.AdvertisementId == advertisementId)
                .OrderBy(d => d.Sequence);

            return destPositionAds;
        }

        public PagedResult<IDestPositionAd> Search(string name, int destinationId, int pageSize, int pageIndex)
        {
            IQueryable<IDestPositionAd> destPositionAds;
            var count = 0;
            if (string.IsNullOrEmpty(name))
            {
                destPositionAds = this._context.DestPositionAd.Include("Advertisement").Include("Position").Include("Destination")
                    .Where(x => x.Destination.DestinationId == destinationId)
                    .OrderByDescending(d => d.CreateDate)
                    .Skip(pageSize * pageIndex).Take(pageSize);
                count = this._context.DestPositionAd.Include("Advertisement").Include("Position").Include("Destination")
                    .Where(x => x.Destination.DestinationId == destinationId).Count();
            }
            else
            {
                destPositionAds = this._context.DestPositionAd.Include("Advertisement").Include("Position").Include("Destination")
                    .Where(x => x.Advertisement.Title.Contains(name) && x.Destination.DestinationId == destinationId)
                    .OrderByDescending(d => d.CreateDate)
                    .Skip(pageSize * pageIndex).Take(pageSize);
                count = this._context.DestPositionAd.Include("Advertisement").Include("Position").Include("Destination")
                    .Where(x => x.Advertisement.Title.Contains(name) && x.Destination.DestinationId == destinationId).Count();
            }

            return BuildPagedResult<IDestPositionAd>(destPositionAds, count);
        }



        public PagedResult<IDestPositionAd> SearchByConfig(string name, int configinfoId, int pageSize, int pageIndex)
        {
            var count = 0;
            if (string.IsNullOrEmpty(name))
            {
                var destPositionAds = this._context.DestPositionAd.Include("Advertisement").Include("Position").Include("Destination")
                    .Where(x => x.Destination.ConfigInfo.ConfigInfoId == configinfoId)
                    .ToList()
                    .Distinct(new DestPositionAdComparer())
                    .OrderByDescending(d => d.CreateDate)
                    .Skip(pageSize * pageIndex).Take(pageSize);
                count = this._context.DestPositionAd.Include("Advertisement").Include("Position").Include("Destination")
                    .Where(x => x.Destination.ConfigInfo.ConfigInfoId == configinfoId)
                    .ToList()
                    .Distinct(new DestPositionAdComparer())
                    .Count();

                return BuildPagedResult<IDestPositionAd>(destPositionAds, count);
            }
            else
            {
                var destPositionAds = this._context.DestPositionAd.Include("Advertisement").Include("Position").Include("Destination")
                    .Where(x => x.Advertisement.Title.Contains(name) && x.Destination.ConfigInfo.ConfigInfoId == configinfoId)
                    .ToList()
                    .Distinct(new DestPositionAdComparer())
                    .OrderByDescending(d => d.CreateDate)
                    .Skip(pageSize * pageIndex).Take(pageSize);
                count = this._context.DestPositionAd.Include("Advertisement").Include("Position").Include("Destination")
                    .Where(x => x.Advertisement.Title.Contains(name) && x.Destination.ConfigInfo.ConfigInfoId == configinfoId)
                   .ToList()
                   .Distinct(new DestPositionAdComparer())
                   .Count();

                return BuildPagedResult<IDestPositionAd>(destPositionAds, count);
            }
        }

        public int CountByName(string name)
        {
            return this._context.DestPositionAd.Include("Advertisement").Include("Position").Include("Destination")
                    .Where(x => x.Advertisement.Title.Contains(name)).Count();
        }

        public IQueryable<IDestPositionAd> FindByAdIdAndPositionIdAndDestId(int adId, int positionId, int destId)
        {
            return this._context.DestPositionAd.Include("Advertisement").Where(x => x.AdvertisementId == adId
                    && x.PositionId == positionId
                    && x.DestinationId == destId);
        }

        public IQueryable<IDestPositionAd> FindAdByDestIdAndPositionId(int destinationId, int positionId)
        {
            return this._context.DestPositionAd.Include("Advertisement")
                .Where(x => x.PositionId == positionId && x.DestinationId == destinationId);
        }
    }

    public class DestPositionAdComparer : IEqualityComparer<IDestPositionAd>
    {

        #region IEqualityComparer<IDestination> 成员

        public bool Equals(IDestPositionAd x, IDestPositionAd y)
        {
            return x.AdvertisementId == y.AdvertisementId;
        }

        #endregion

        #region IEqualityComparer<IDestPositionAd> 成员


        public int GetHashCode(IDestPositionAd obj)
        {
            return obj.ToString().GetHashCode();
        }

        #endregion
    }
}
